<?php

//    IRC PHP CLASS

//    Copyright (C) 2009 Kirth 'Sebastian'.
//
//    This code is property of the CodeArray PHP project.  Rights Reserved!
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


set_time_limit(60); // 60 second runs.

require_once 'requirements/socket.class.php';


class IRC
{
	static	$host;
	static	$port;
	static  $chan;
	
	public  $nick;
	
	private $sock;
	
	public function __construct($conf) {
		$this->host = $conf['host'];
		$this->port = $conf['port'];
		$this->chan = $conf['chan'];
		
		$this->sock = new Socket($this->host, $this->port);
		$this->Authenticate();
		
		
		$this->Say("Sieg PHP!");
		$this->Action("dances with PHP");
			
			$this->Main();  // Initiate the grand, main loop!

		
	}
	
	private function Main() {
		while(1) {
			while($d = $this->sock->Read()) {
				echo nl2br($d);
				flush();
			}
		}
	}
	
	private function Authenticate() {
		$authline = "USER %s 127.0.0.1 WINLORD MU:John Mullins";
		$this->sock->WriteLine(sprintf($authline, $nick));
		$this->sock->WriteLine("NICK MulBot");
		$this->sock->WriteLine("JOIN " . $this->chan);
	}
	
	public function Say($w) {
		if($this->sock->WriteLine('PRIVMSG ' . $this->chan . ' :' . $w)) {
			return true;
		}
		
			return false;
	}
	
	public function Action($a) {
		if($this->sock->WriteLine('PRIVMSG ' . $this->chan . ' :' . (char)0x01 . "ACTION " .$a)) {
			return true;
		}
		
			return false;
	}
	
	

}

?>

<?php

// Example use for testing.
$conf = array('host' => 'irc.wyldryde.org',
			  'port' =>  6667,
			  'chan' => '#dreamincode',
			  'nick' => 'Mulbot',
			 );

$irc = new IRC($conf);

?>